package com.linewell.service.impl;

import com.linewell.dao.UserInfoMapper;
import com.linewell.entity.Administrator;
import com.linewell.entity.UserInfo;
import com.linewell.service.IUserService;
import com.linewell.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Title: ssm2
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/1/5 11:18
 */
@Service
public class UserService implements IUserService {

    @Autowired
    UserInfoMapper userInfoDao;

    public List<UserInfo> checkPunsterName(String punsterName){
        return userInfoDao.checkPunsterName(punsterName);
    }

    public List<UserInfo> checkNamePwd(String punsterName, String password) {
        return userInfoDao.checkNamePwd(punsterName,password);
    }

    public boolean saveUserInfo(UserInfo userInfo){
        return userInfoDao.savaUserInfo(userInfo);
    }

    public boolean uploadUserPicture(String picture,Long userId){
        return userInfoDao.uploadUserPicture(picture,userId);
    }

    public Msg getHotUser(int page) {
        List<UserInfo> userInfoList = userInfoDao.getHotUser(page);
        if (userInfoList.size() > 0){
            return Msg.success().add("userInfoList",userInfoList);
        } else {
            return Msg.fail().add("msg","没有更多信息了");
        }

    }

    public Administrator checkAdmin(String punsterName, String password) {
        return userInfoDao.checkAdmin(punsterName,password);
    }

    public Msg updataUserInfo(String userId, String nickName, String sex, String birthday, String userEmail, String myself) {
        if (userInfoDao.updataUserInfo(userId,nickName,sex,birthday,userEmail,myself)) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public Msg changePwd(Long userId,String oldPwd, String newPwd) {
        if (userInfoDao.checkPwd(userId,oldPwd).size() == 0){
            return Msg.fail().add("msg","密码不正确");
        } else if (userInfoDao.updatePwd(userId,newPwd)){
            return Msg.success();
        } else {
            return Msg.fail().add("msg","未知错误，暂无法修改密码");
        }
    }

    public List<UserInfo> getUsers(int page, int limit) {
        return userInfoDao.getUsers(page,limit);
    }

    public int getUserCount() {
        return userInfoDao.getUserCount();
    }

    public List<UserInfo> getUsersByPunsterName(String keyword) {
        return userInfoDao.getUsersByPunsterName(keyword);
    }

    public Msg deleteAllUser(String userIds) {
        if (userInfoDao.deleteAllUser(userIds)) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public Msg deleteUserByUserId(Long userId) {
        if (userInfoDao.deleteUserByUserId(userId)) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public Msg updateUserState(Long userId, int state) {
        if (userInfoDao.updateUserState(userId,state)) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public Msg updateUser(String field, String value, Long userId) {
        if (userInfoDao.updateUser(field,value,userId)) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public UserInfo checkEmail(String punsterName, String email) {
        return userInfoDao.checkEmail(punsterName,email);
    }

    public UserInfo getUserByUserId(Long userId) {
        return userInfoDao.getUserByUserId(userId);
    }

    public Msg changePwdByCode(Long userId, String newPwd) {
        if (userInfoDao.updatePwd(userId,newPwd)){
            return Msg.success();
        } else {
            return Msg.fail().add("msg","未知错误密码修改失败");
        }

    }
}
